#/bin/bash
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

#if [[ "$(whoami)" != "root" ]]; then
#       echo "please run this script as root" >&2
#       exit 1
#fi


find /home/mysql_backup/sysdb -type d -mtime +10  | xargs -i ls -l {}
find /home/mysql_backup/sysdb -type d -mtime +10  | xargs -i rm -rf {}

find /home/mysql_backup/sysdb -type d -mtime +10  | xargs -i ls -l {}

bak_path=/home/mysql_backup/sysdb
mysql_login="mysql -uroot -p<PASSWORD> -h10.64.2.22"
Mysqldump="mysqldump -uroot -p<PASSWORD> -h10.64.2.22 -F --lock-all-tables --events --flush-privileges --triggers --routines --hex-blob -B"
Email_address=(chaoli.ye@windfindtech.com jc.wu@windfindtech.com)

mkdir -p ${bak_path}/$(date +%F)

data=${bak_path}/backup_data_$(date +%Y%m%d).sql.gz

Log_file=${bak_path}/$(date +%F)/backup_$(date +%F).log

for database in `${mysql_login} -e "show databases;" | sed -e '1d' | grep -P "IGD_SiteSync|WF_sites_gd|joomla_GD|piwik|Light_Manager|Light_Service"`
do
	echo $(date "+%F %H:%M:%S") ${database} >> ${Log_file}
        $Mysqldump $database | gzip > ${bak_path}/$(date +%F)/${database}_$(date +%F).sql.gz
done

[ $(grep -Pvc "^\d" ${Log_file}) -gt 0 ] && cat ${Log_file} | mutt -s "test" ${Email_address[*]}
